package reflect;

import java.lang.reflect.Field;

/**
 * @autor my
 * 2017/9/13.
 */
public class UseCommentCreatObjct {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        //获取类对象
        Class<Car> carClass = Car.class;
        //获取类注解
        Comment comment = carClass.getDeclaredAnnotation(Comment.class);
        if (comment!=null){
            String color=comment.color();
            Car car=null;
            try{
                car=carClass.newInstance();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
            //获取属性
            Field field=carClass.getDeclaredField("color");
            field.setAccessible(true);
            field.set(car,color);
            //打印对象，和属性
            System.out.println(car);
            System.out.println(car.getColor());
        }
        else {
            System.out.println("对象不存在");
        }
    }
}
